/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.Dialog;

import controllers.entity.Children;
import controllers.process.CanBoProcess;
import controllers.process.ChildrenProcess;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import models.Constant;
import views.JPanel.TTCanBo;

/**
 *
 * @author HoaDuyen
 */
public class DlgThemChildren extends java.awt.Dialog {

    /**
     * Creates new form DlgThemChildren
     */
    private CanBoProcess canBoProcess;
    private ChildrenProcess childrenProcess;
    private int macanbo;
    private Children children;
    private TTCanBo parent;
    public DlgThemChildren(JFrame parent, boolean modal,int macanbo,Children children ) {
        super(parent, modal);
        this.parent=(TTCanBo)parent;
        this.children=children;
        this.setResizable(false);
        this.setLocation(Constant.LOCATE_XDLG, Constant.LOCATE_YDLG);
        this.macanbo=macanbo;
        canBoProcess=new CanBoProcess();
        childrenProcess=new ChildrenProcess();
        
        initComponents();
        okchildren.setText("Thêm");
        if (children != null) { // Dang o che do sua
            tencon.setText(children.getHoTen());
            ngaysinhcon.setDate(children.getNgaySinh());
            //goitinh.setText(children.getGoiTinh());
            sexcb.setSelectedItem(children.getGoiTinh());
            thanhtichcon1.setText(children.getThanhTich());
            txtGiaDinh.setText("Sửa thông tin");
            okchildren.setText("Lưu");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        tencon = new javax.swing.JTextField();
        ngaysinhcon = new com.toedter.calendar.JDateChooser();
        thanhtichcon1 = new javax.swing.JTextField();
        gioitinh = new javax.swing.JLabel();
        sexcb = new javax.swing.JComboBox();
        okchildren = new javax.swing.JButton();
        cancelcon = new javax.swing.JButton();
        txtGiaDinh = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel9.setText("Họ tên ");

        jLabel10.setText("Năm sinh");

        jLabel13.setText("Thành tích");

        gioitinh.setText("Giới tính");

        sexcb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));

        javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel12Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                        .addComponent(tencon, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel12Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addGap(49, 49, 49)
                        .addComponent(thanhtichcon1))
                    .addGroup(jPanel12Layout.createSequentialGroup()
                        .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(gioitinh))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ngaysinhcon, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sexcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(34, 34, 34))
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tencon, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ngaysinhcon, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gioitinh)
                    .addComponent(sexcb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(thanhtichcon1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        okchildren.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_add.png"))); // NOI18N
        okchildren.setText("Thêm");
        okchildren.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okchildrenActionPerformed(evt);
            }
        });

        cancelcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_delete.png"))); // NOI18N
        cancelcon.setText("Hủy");
        cancelcon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelconActionPerformed(evt);
            }
        });

        txtGiaDinh.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        txtGiaDinh.setText("Thêm thành viên");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(okchildren)
                .addGap(18, 18, 18)
                .addComponent(cancelcon)
                .addGap(78, 78, 78))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(128, 128, 128)
                        .addComponent(txtGiaDinh))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jPanel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addComponent(txtGiaDinh)
                .addGap(18, 18, 18)
                .addComponent(jPanel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okchildren)
                    .addComponent(cancelcon))
                .addGap(24, 24, 24))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void cancelconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelconActionPerformed
        dispose();
    }//GEN-LAST:event_cancelconActionPerformed

    private void okchildrenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okchildrenActionPerformed
        if (tencon.getText().length()>29){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tencon.selectAll();
            tencon.requestFocus();
            return;
        }
        if (thanhtichcon1.getText().length()>29){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            thanhtichcon1.selectAll();
            thanhtichcon1.requestFocus();
            return;
        }
        String thoten = tencon.getText();
        String tthanhtich=thanhtichcon1.getText();
        int dem = ChildrenProcess.getCountConCai() + 1;
        System.out.println(dem);
        String tmaconcai=dem+"";
        System.out.println(dem + " " + tmaconcai);
        String tgoitinh=sexcb.getSelectedItem().toString();
        Date tnamsih= ngaysinhcon.getDate();
        if (tnamsih == null) {
            JOptionPane.showMessageDialog(this,"Không được bỏ trống ngày sinh !");
            return;
        }
        java.util.Date today = new java.util.Date();
        if (today.getTime() - tnamsih.getTime() < 0){
            JOptionPane.showMessageDialog(this, "Lỗi ! Ngày sinh sau ngày hôm nay !");
            return;
        }
        if ((today.getTime() - tnamsih.getTime())/31536000 >80*1000){
            JOptionPane.showMessageDialog(this, "Lỗi ! Tuổi không hợp lý (>50 tuổi) !");
            return;
        }
        if (thoten.equals("") || tnamsih== null ) {
            JOptionPane.showMessageDialog(this, "Bạn phải nhập đầy đủ,chính xác!");
        } else {
            Children ct=new Children(macanbo,tmaconcai,thoten,tnamsih,tthanhtich,tgoitinh);
            if (children == null) { // Them moi sach
                if (ChildrenProcess.insertChildren(ct)) {
                    JOptionPane.showMessageDialog(this, "Thêm thành công!");
                    parent.loadListGiaDinh();
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "Trùng mã quản lý !");
                }
            } else {
                //sua sach
                ct.setMaConCai(this.children.getMaConCai());
                if (ChildrenProcess.updateChildren(ct)) {
                    JOptionPane.showMessageDialog(this, "Sửa thành công!");
                    parent.loadListGiaDinh();
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "Lỗi !");
                }
            }
        }
    }//GEN-LAST:event_okchildrenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelcon;
    private javax.swing.JLabel gioitinh;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel12;
    private com.toedter.calendar.JDateChooser ngaysinhcon;
    private javax.swing.JButton okchildren;
    private javax.swing.JComboBox sexcb;
    private javax.swing.JTextField tencon;
    private javax.swing.JTextField thanhtichcon1;
    private javax.swing.JLabel txtGiaDinh;
    // End of variables declaration//GEN-END:variables
}
